package net.xxcl.kygl.controller;

import net.xxcl.kygl.pojo.Criteria;
import net.xxcl.kygl.pojo.ProjectType;
import net.xxcl.kygl.service.ProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 夏汛
 * Date: 12-5-13
 * Time: 下午10:36
 */
@Controller
@RequestMapping("/projecttype")
public class ProjectTypeController {

    @Autowired
    private ProjectTypeService projectTypeService;

    /**
     * 获取课题类型分页
     * @param start
     * @param limit
     * @return
     */
    @RequestMapping(value = "/list",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> list(@RequestParam(value = "start",required = false) Integer start,
                                                 @RequestParam(value = "limit",required = false) Integer limit,
                                                 HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        if(request.getParameter("inSchool")!=null){
            criteria.put("inSchool",1);
        }
        if(request.getParameter("outSchool")!=null){
            criteria.put("outSchool",1);
        }
        criteria.put("iscross",0);
        int total = projectTypeService.countByExample(criteria);
        if(start!=null && limit!=null){
            criteria.setMysqlOffset(start);
            criteria.setMysqlLength(limit);
        }
        List<ProjectType> list = projectTypeService.selectByExample(criteria);
        responseMap.put("total",total);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 新增和修改课题
     * @param projecttype
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> saveOrUpdate(@ModelAttribute("projecttype") ProjectType projecttype){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try{
            //新增课题类型
            if(projecttype.getId()==null){
                projectTypeService.insert(projecttype);
                responseMap.put("method", "Create");
                responseMap.put("success", "true");
                responseMap.put("info", "新增成功！");
            }
            //编辑课题类型
            else {
                projectTypeService.updateByPrimaryKey(projecttype);
                responseMap.put("success", "true");
                responseMap.put("info", "编辑成功！");
            }
        } catch (Exception e){
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
        }
        return responseMap;
    }

    /**
     * 删除课题类型
     * @param ids
     * @return
     */
    @RequestMapping(value="/remove",method=RequestMethod.POST)
    public @ResponseBody Map<String,Object> remove(@RequestParam("ids") String ids){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try {
            projectTypeService.deleteProjectType(ids);
            responseMap.put("success", "true");
            responseMap.put("info", "删除成功！");
            return responseMap;
        }catch(Exception e) {
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
            return responseMap;
        }
    }
}
